package com.yilin.platform.controller;

import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.interceptor.EncryptInterceptor;
import com.yilin.platform.base_core.interceptor.SkipLoginVerify;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.entity.user.deserialize.Account;
import com.yilin.platform.service.IUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/platform/user/")
public class UserController extends BaseController {
    @Resource
    private IUserService userService;

    /**
     * 获取登录信息
     * */
    @PostMapping(value = "login")
    @EncryptInterceptor
    @SkipLoginVerify
    public ReturnData<Account> login(String username,String customerKey){
        ParamUtil.isNull(username,"请输人账号");
        ParamUtil.isNull(customerKey,"请输入授权key");
        Account account = userService.loginToYilin(username,customerKey);
        return jsonView(true, "获取成功",null, account.getToken());
    }

    /**
     * 获取登录信息
     * */
    @PostMapping(value = "getLoginUserInfo")
    @EncryptInterceptor
    public ReturnData<Account> getLoginUserInfo(){
        if(getLoginUser() == null) return jsonView(NO_LOGIN, "登录失效");
        return jsonView(true, "获取成功",getLoginUser(), getLoginUser().getToken());
    }

    /**
     * 获取平台地址
     * */
    @PostMapping(value = "getPlatWebUrl")
    @EncryptInterceptor
    @SkipLoginVerify
    public ReturnData<String> getPlatWebUrl(){
        String platUrl = userService.getYilinWebUrl();
        return jsonView(true, "获取成功",platUrl);
    }

    @GetMapping("platUrl")
    @EncryptInterceptor
    public String index(Model model) {
       return  userService.getYilinBaseUrl();
    }
}
